#!/usr/bin/python
import os
import sys
import urllib
import urllib2
import cookielib
from time import sleep
from lxml import etree

def download(link, localDirectory="./"):
    while 1:
        print "fetching user page %s%s"%(" "*40, "[\033[1;34mwait\033[1;m]")
        response = urllib2.urlopen(link)
        print "fetching user page %s%s"%(" "*40, "[\033[1;32mdown\033[1;m]")
        hxs = etree.HTML(response.read())
        ff = hxs.xpath('//form[@id="ff"]')
        #TODO this script is only for free user right now...

        waitURL = ff[0].values()[1]
        #TODO use key to get the value of action rather than index...
        str=urllib.urlencode({'dl.start':'Free'})
        print "fetching waiting page %s%s"%(" "*37, "[\033[1;34mwait\033[1;m]")
        response = urllib2.urlopen(waitURL, str)
        print "fetching waiting page %s%s"%(" "*37, "[\033[1;32mdown\033[1;m]")
        downpageHTML = response.read()

        error = downpageHTML.find("Instant")
        if error == -1:
            start = downpageHTML.find("<form name=\"dlf\"")
            end = downpageHTML.find("method=\"post\"")
            #TODO a better way to get the real url of the file
            urlcontainer = downpageHTML[start:end]

            downloadURL = urlcontainer[urlcontainer.find("http"):-2]
            for i in range(75):
                print "*",
                sys.stdout.flush()
                sleep(1)
            #TODO read the waiting time from downpage's javascript

            print ""
            print downloadURL
            print localDirectory
            os.system("wget %s -P %s"%(downloadURL,localDirectory))
            #TODO aria2c need to be tested
            #TODO wget args
            break
        else:
            wtcontainer = downpageHTML[downpageHTML.find("Instant download access! Or try again in about"):downpageHTML.find("Create Premium Account")]
            wt = wtcontainer[47:-18]
            print "ERROR can not get download url waiting for %s minutes"%wt
            sleep(float(wt)*60)

if __name__ == "__main__":
    list = raw_input("Please input the list name:")
    fh = open(list,'r')
    links = fh.readlines()
    localDir = raw_input("Please input download dir name (./ by default):")
    for url in links:
        if not url == "\n":
            download(url, localDir);
